The DataModel class
DataModel
Bases: BaseModel, SynalinksSaveable, DataModelOperators
The backend-dependent data model.
This data model uses Pydantic to provide, JSON schema inference and JSON serialization.
Examples:
Creating a DataModel for structured output
class AnswerWithReflection(synalinks.DataModel):
thinking: str = synalinks.Field(
description="Your step by step thinking",
)
reflection: str = synalinks.Field(
description="The reflection about your thinking",
)
answer: str = synalinks.Field(
description="The correct answer",
)
language_model = synalinks.LanguageModel("ollama/mistral")
generator = synalinks.Generator(
data_model=AnswerWithReflection,
language_model=language_model,
)
Source code in synalinks/src/backend/pydantic/core.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
__getitem__(key)
Get item wrapper to make it easier to access JSON fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to access. |
required |
get(key, default=None)
Get wrapper to make it easier to access JSON fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to access. |
required |
default
|
any
|
The default value if key not found. |
None
|
Source code in synalinks/src/backend/pydantic/core.py
get_json()
Gets the JSON value of the data model.
Optional fields whose value is None are omitted from the output;
the schema still declares them, so consumers should treat a missing
key as None.
Serialization is duck-typed (serialize_as_any): a field declared
with a parent type (e.g. List[Entity]) dumps each item with the
instance's fields, so Entity/Relation subclasses keep their
properties inside a generic KnowledgeGraph.
Returns:
| Type | Description |
|---|---|
dict
|
The JSON value. |
Source code in synalinks/src/backend/pydantic/core.py
get_nested_entity(key)
Retrieve a nested Entity and convert it to a JsonDataModel.
The entity's label field is used as the discriminator to look up
its schema in this model's $defs. Returns None when the value
at key has no label or when the schema cannot be resolved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The field name holding the nested entity. |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | None
|
A typed JsonDataModel wrapping the
nested entity, or |
Source code in synalinks/src/backend/pydantic/core.py
get_nested_entity_list(key)
Retrieve a nested Entity list and convert it to typed JsonDataModels.
Each item is resolved against this model's $defs using its
label field as discriminator. Items without a label field
or an unresolved schema are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The field name holding the list of nested entities. |
required |
Returns:
| Type | Description |
|---|---|
list[JsonDataModel]
|
One JsonDataModel per resolved entity. |
Source code in synalinks/src/backend/pydantic/core.py
get_schema()
classmethod
Gets the JSON schema of the data model.
Returns:
| Type | Description |
|---|---|
dict
|
The JSON schema. |
keys()
classmethod
Gets the JSON properties keys of the data model.
Returns:
| Type | Description |
|---|---|
dict
|
The JSON schema. |
Source code in synalinks/src/backend/pydantic/core.py
prettify_json()
Get a pretty version of the JSON object for display.
Returns:
| Type | Description |
|---|---|
str
|
The indented JSON object. |
Source code in synalinks/src/backend/pydantic/core.py
prettify_schema()
classmethod
Get a pretty version of the JSON schema for display.
Returns:
| Type | Description |
|---|---|
str
|
The indented JSON schema. |
Source code in synalinks/src/backend/pydantic/core.py
to_json_data_model(name=None)
Converts the data model to a backend-independent data model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Optional. The name of the json data model. If None, a name will be given automatically. |
None
|
Returns:
| Type | Description |
|---|---|
JsonDataModel
|
The backend-independent data model. |
Source code in synalinks/src/backend/pydantic/core.py
to_symbolic_data_model(name=None)
classmethod
Converts the data model to a symbolic data model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Optional. The name of the symbolic data model. If None, a name will be given automatically. |
None
|
Returns:
| Type | Description |
|---|---|
SymbolicDataModel
|
The symbolic data model. |
Source code in synalinks/src/backend/pydantic/core.py
MetaDataModel
Bases: DataModelOperators, DataModelTransforms, type(BaseModel)
The metaclass data model.
This class defines operations at the metaclass level.
Allowing to use Synalinks Python operators with DataModel types.
Source code in synalinks/src/backend/pydantic/core.py
any_data_model(args=None, kwargs=None)
Check if any of the arguments are backend-dependent data models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
tuple
|
Optional. The positional arguments to check. |
None
|
kwargs
|
dict
|
Optional. The keyword arguments to check. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if any of the arguments are meta classes, False otherwise. |
Source code in synalinks/src/backend/pydantic/core.py
any_meta_class(args=None, kwargs=None)
Check if any of the arguments are meta classes.
This happen when using a DataModel without instanciating it.
In Synalinks this is used when declaring data models for schema inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
tuple
|
Optional. The positional arguments to check. |
None
|
kwargs
|
dict
|
Optional. The keyword arguments to check. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if any of the arguments are meta classes, False otherwise. |
Source code in synalinks/src/backend/pydantic/core.py
is_data_model(x)
Returns whether x is a DataModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
any
|
The object to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if |
is_meta_class(x)
Returns whether x is a meta class.
A meta class is a python type. This method checks if the data model provided
if a meta class, allowing to detect if the DataModel have been instanciated.
Meta classes are using in Synalinks when declaring data models for schema inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
any
|
The object to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in synalinks/src/backend/pydantic/core.py
The Python operators of the data models, defined once.
SymbolicDataModel, JsonDataModel, DataModel and its metaclass
MetaDataModel share these operators; each only says how to run an op
(_apply_op). See the synalinks.ops tables for the exact semantics.
DataModelOperators
Python operators (+, &, |, ^, ~, in) on data models.
Source code in synalinks/src/backend/common/data_model_operators.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
__add__(other)
Concatenates this data model with another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
JsonDataModel | DataModel | SymbolicDataModel
|
The other data model to concatenate with. |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel
|
The concatenated data model, symbolic if one of them is a metaclass or symbolic data model. |
Source code in synalinks/src/backend/common/data_model_operators.py
__and__(other)
Perform a logical_and with another data model.
If one of them is None, output None. If both are provided,
then concatenates this data model with the other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
JsonDataModel | DataModel | SymbolicDataModel
|
The other data model to concatenate with. |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel | None
|
The concatenated data
model or |
Source code in synalinks/src/backend/common/data_model_operators.py
__contains__(other)
Check if the schema of other is contained in this one,
or if a string key exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
SymbolicDataModel | DataModel | str
|
The other data model to compare with, or a string key to check for in the schema properties. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if all properties of |
Source code in synalinks/src/backend/common/data_model_operators.py
__invert__()
Perform an inversion/negation (logical_not).
Returns:
| Type | Description |
|---|---|
SymbolicDataModel | None
|
|
Source code in synalinks/src/backend/common/data_model_operators.py
__or__(other)
Perform a logical_or with another data model.
If one of them is None, output the other one. If both are provided,
then concatenates this data model with the other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
JsonDataModel | DataModel | SymbolicDataModel
|
The other data model to concatenate with. |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel | None
|
The concatenation if
both are provided, the non- |
Source code in synalinks/src/backend/common/data_model_operators.py
__radd__(other)
Concatenates (reverse) another data model with this one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
JsonDataModel | DataModel | SymbolicDataModel
|
The other data model to concatenate with. |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel
|
The concatenated data model, symbolic if one of them is a metaclass or symbolic data model. |
Source code in synalinks/src/backend/common/data_model_operators.py
__rand__(other)
Perform a logical_and (reverse) with another data model.
If one of them is None, output None. If both are provided,
then concatenates the other data model with this one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
JsonDataModel | DataModel | SymbolicDataModel
|
The other data model to concatenate with. |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel | None
|
The concatenated data
model or |
Source code in synalinks/src/backend/common/data_model_operators.py
__ror__(other)
Perform a logical_or (reverse) with another data model.
If one of them is None, output the other one. If both are provided,
then concatenates the other data model with this one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
JsonDataModel | DataModel | SymbolicDataModel
|
The other data model to concatenate with. |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel | None
|
The concatenation if
both are provided, the non- |
Source code in synalinks/src/backend/common/data_model_operators.py
__rxor__(other)
Perform a logical_xor (reverse) with another data model.
If one of them is None, output the other one. If both are provided,
then the output is None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
JsonDataModel | DataModel | SymbolicDataModel
|
The other data model. |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel | None
|
|
Source code in synalinks/src/backend/common/data_model_operators.py
__xor__(other)
Perform a logical_xor with another data model.
If one of them is None, output the other one. If both are provided,
then the output is None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
JsonDataModel | DataModel | SymbolicDataModel
|
The other data model. |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel | None
|
|
Source code in synalinks/src/backend/common/data_model_operators.py
DataModelTransforms
Schema transforms (factorize, masks, prefix, suffix) on data models.
Kept apart from DataModelOperators so a DataModel instance does not
get these names, which would shadow fields of the same name.
Source code in synalinks/src/backend/common/data_model_operators.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
factorize()
Factorizes the data model.
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel
|
The factorized data model. |
Source code in synalinks/src/backend/common/data_model_operators.py
in_mask(mask=None, pattern=None, recursive=True)
Applies a mask to keep only specified keys of the data model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
list
|
The mask to be applied (list of keys). |
None
|
pattern
|
str
|
Optional. A regex pattern to match keys to keep. |
None
|
recursive
|
bool
|
Optional. Whether to apply the mask recursively.
Defaults to |
True
|
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel
|
The data model with the mask applied. |
Source code in synalinks/src/backend/common/data_model_operators.py
out_mask(mask=None, pattern=None, recursive=True)
Applies a mask to remove specified keys of the data model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
list
|
The mask to be applied (list of keys). |
None
|
pattern
|
str
|
Optional. A regex pattern to match keys to remove. |
None
|
recursive
|
bool
|
Optional. Whether to apply the mask recursively.
Defaults to |
True
|
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel
|
The data model with the mask applied. |
Source code in synalinks/src/backend/common/data_model_operators.py
prefix(prefix=None)
Add a prefix to all the data model fields (non-recursive).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
the prefix to add |
None
|
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel
|
The data model with the prefix added. |
Source code in synalinks/src/backend/common/data_model_operators.py
suffix(suffix=None)
Add a suffix to all the data model fields (non-recursive).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
suffix
|
str
|
the suffix to add |
None
|
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel
|
The data model with the suffix added. |
Source code in synalinks/src/backend/common/data_model_operators.py
apply_symbolic_op(self, op, *args)
Run op symbolically on args (the _apply_op of symbolic models).